curl 工具
curl 工具
curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。
不带有任何参数时,curl 就是发出 GET 请求。
curl https://www.example.com
指定请求类型
-X
参数指定 HTTP 请求的方法。
$ curl -X POST https://www.example.com
设置请求头
-H
参数添加 HTTP 请求的标头。
$ curl -H 'Accept-Language: en-US' https://google.com
上面命令添加 HTTP 标头 Accept-Language: en-US
。
$ curl -H 'Accept-Language: en-US' -H 'Secret-Message: xyzzy' https://google.com
上面命令添加两个 HTTP 标头。
$ curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
上面命令添加 HTTP 请求的标头是 Content-Type: application/json
,然后用 -d
参数发送 JSON 数据。
设置请求的客户端
-A
参数指定客户端的用户代理标头,即 User-Agent。
curl 的默认用户代理字符串是 curl/[version]
curl -A 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' https://google.com
上面命令将 User-Agent 改成 Chrome 浏览器。
curl -A '' https://google.com
上面命令会移除 User-Agent 标头。
也可以通过 -H
参数直接指定标头,更改 User-Agent。
curl -H 'User-Agent: php/1.0' https://google.com
发送 Cookie
-b
参数用来向服务器发送 Cookie
curl -b 'foo=bar' https://google.com
上面命令会生成一个标头 Cookie: foo=bar
,向服务器发送一个名为foo、值为bar的 Cookie。
curl -b 'foo1=bar;foo2=bar2' https://google.com
上面命令发送两个 Cookie。
curl -b cookies.txt https://www.google.com
上面命令读取本地文件 cookies.txt,里面是服务器设置的 Cookie(参见 -c
参数),将其发送到服务器。
-c
参数将服务器设置的 Cookie 写入一个文件。
curl -c cookies.txt https://www.google.com
上面命令将服务器的 HTTP 回应所设置 Cookie 写入文本文件 cookies.txt
上传二进制文件
-F
参数用来向服务器上传二进制文件。
$ curl -F 'file=@photo.png' https://google.com/profile
上面命令指定 MIME 类型为 image/png
,否则 curl 会把 MIME 类型设为 application/octet-stream
。
-F
参数也可以指定文件名。
$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
上面命令中,原始文件名为 photo.png
,但是服务器接收到的文件名为 me.png
。
添加请求体
-d
参数用于发送 POST 请求的数据体。
$ curl -d'login=emma&password=123'-X POST https://google.com/login
# 或者
$ curl -d 'login=emma' -d 'password=123' -X POST https://google.com/login
使用 -d
参数以后,HTTP 请求会自动加上标头 Content-Type : application/x-www-form-urlencoded
。并且会自动将请求转为 POST 方法,因此可以省略 -X POST
。
-d
参数可以读取本地文本文件的数据,向服务器发送。
$ curl -d '@data.txt' https://google.com/login
上面命令读取 data.txt
文件的内容,作为数据体向服务器发送。
--data-urlencode
参数等同于 -d
,发送 POST 请求的数据体,区别在于会自动将发送的数据进行 URL 编码。
$ curl --data-urlencode 'comment=hello world' https://google.com/login
上面代码中,发送的数据 hello world
之间有一个空格,需要进行 URL 编码。
发送 JSON 数据
curl -H "Content-Type:application/json" \
-H "Data_Type:msg" \
-X POST \
--data '{"dmac": "00:0C:29:EA:39:70", "alert_type": "alarm", "risk": 2, "trojan_name": "Trojan.qq3344", "smac": "00:0C:29:EA:39:66", "sub_alert_type": "trojan", "sport": 11, "id": "153189767146", "desc": "NoSecure 1.2 \u6728\u9a6c\u53d8\u79cd4\u8fde\u63a5\u64cd\u4f5c", "sip": "62.4.07.18", "dip": "139.82.31.91", "rule_id": 123451, "trojan_type": 4, "time": "2018-07-18 15:07:51", "dport": 61621, "detector_id": "170301020011", "os": "Windows", "trojan_id": 50030}' http://127.0.0.1:5000/service
添加请求的来源
-e
参数用来设置 HTTP 的标头Referer,表示请求的来源。
curl -e 'https://google.com?q=example' https://www.example.com
上面命令将 Referer 标头设为 https://google.com?q=example
-H
参数可以通过直接添加标头 Referer,达到同样效果。
curl -H 'Referer: https://google.com?q=example' https://www.example.com
设置请求代理
-x
参数指定 HTTP 请求的代理。
$ curl -x socks5://james:cats@myproxy.com:8080 https://www.example.com
上面命令指定 HTTP 请求通过 myproxy.com:8080
的 socks5 代理发出。
如果没有指定代理协议,默认为 HTTP。
$ curl -x james:cats@myproxy.com:8080 https://www.example.com
上面命令中,请求的代理使用 HTTP 协议。
保存文件
-o
参数将服务器的回应保存成文件,等同于 wget 命令。
$ curl -o example.html https://www.example.com
上面命令将 www.example.com
保存成 example.html
。
-O
参数将服务器回应保存成文件,并将 URL 的最后部分当作文件名。
$ curl -O https://www.example.com/foo/bar.html
上面命令将服务器回应保存成文件,文件名为 bar.html
。